Skip to content

fix(start-plan): working JWT auth, system blocks, and jsdom captcha - #5

Closed
Wraient wants to merge 3 commits into
TriDefender:auto-captchafrom
Wraient:fix/start-plan-working
Closed

Wraient wants to merge 3 commits into
TriDefender:auto-captchafrom
Wraient:fix/start-plan-working

Conversation

@Wraient

@Wraient Wraient commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #2 — makes the auto-captcha branch actually work for ZCode Start Plan (plan: start-plan).

The original auto-captcha branch had the right reactive-captcha idea but failed in practice:

  • Default config pointed at coding-plan + placeholder API key → 401 token expired or incorrect
  • Upstream used x-api-key for JWT instead of Authorization: Bearer
  • Playwright solver never called startTracelessVerification()
  • Missing official ZCode system gateway blocks → 3012 even with valid captcha

This PR ports and hardens the approach from community testing (see issue #2 / PR #4), verified live:

  1. auth import-jwt — decrypt JWT from ~/.zcode/v2/credentials.json
  2. Official system blocks — zcode_system.json injected on every start-plan request
  3. Proactive jsdom captcha — captcha_node/solver.js + Aliyun traceless verification (scene 11xygtvd), cached ~45s
  4. Bearer JWT to zcode.z.ai/api/v1/zcode-plan/anthropic/v1/messages
  5. Single captcha retry on rejection; 3012 surfaced as account_blocked (not misclassified as captcha)

Addresses TriDefender review feedback on PR #4:

  • solveCaptcha wrapped in try/catch on retry path
  • Lazy npm install in captcha_node (no postinstall hook)
  • Narrow captcha failure detection (no blanket 403/405 retry)
  • Safe system-prompt normalization for malformed blocks

Test plan

  • bun test — 174 tests pass
  • node captcha_node/solver.js — produces VERIFY_PARAM=...
  • Live: auth import-jwt + plan: start-plan + POST /v1/messages → HTTP 200, model reply "ok" (~7s TTFB)
  • Billing before/after test: 200 (no 3012)

Usage

bun run src/index.ts auth import-jwt
# config.yaml: auth.mode: oauth, plan: start-plan
bun run src/index.ts

Requires Node.js for captcha solver (auto-installed on first solve).

Start Plan on zcode.z.ai requires Bearer JWT, official ZCode system gateway
blocks, and proactive Aliyun traceless captcha — not the coding-plan API key path.

- Replace broken Playwright reactive solver with jsdom traceless captcha
- Inject official system blocks (fixes 3012 without captcha)
- Bearer JWT upstream auth; auth import-jwt from ZCode desktop credentials
- Proactive captcha before request with single retry on rejection
- Document start-plan setup in README and config.example.yaml

Fixes #2. Tested live: POST /v1/messages returns 200 with glm-5.2.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 525ff04bcc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +29 to +31
if (parsed.stream !== true) {
parsed.stream = true;
changed = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve non-streaming start-plan semantics

When a /v1/messages start-plan client omits stream or sends stream:false, this rewrite forces the upstream request to stream, while the handler later only passes the upstream response through. That breaks the documented non-streaming Anthropic endpoint and the new README start-plan curl example by returning text/event-stream instead of a JSON message; either preserve the client’s stream flag or aggregate the SSE back into a non-stream response.

Useful? React with 👍 / 👎.

Comment thread src/proxy/handler.ts
Comment on lines +175 to +177
const needsRetry =
detectCaptchaChallenge(opts.upstreamResp) !== null ||
(await isCaptchaFailure(opts.upstreamResp));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid draining successful SSE responses for captcha checks

For every successful start-plan response without a captcha header, this awaits isCaptchaFailure(), which clones and reads the entire response body. On SSE responses this means proxyRequest does not return the streaming Response to the client until the upstream stream finishes, so streaming consumers receive no tokens until completion or can hang on long streams; restrict body scanning to error statuses/non-SSE responses before calling text().

Useful? React with 👍 / 👎.

New accounts need Start Plan balance buckets provisioned by the ZCode host
process — OAuth alone leaves billing/balance empty (1005 exceed quota).

auth onboard zai: OAuth login → encrypt-write ~/.zcode credentials →
sync config.json start-plan JWT → launch ZCode → poll billing until buckets
exist → save proxy credentials.
@TriDefender

Copy link
Copy Markdown
Owner

Thanks for this PR, it's partially accepted and merged. Contribution attributed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants